//
//  YbrMusicItem.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-5.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrMusicItem.h"
#import "YbrTextClassInfoItem.h"
#import "YbrTextClassMusicItem.h"
#import "YbrTimerUtil.h"

@implementation YbrMusicItem



+(YbrMusicItem *) classInfoToMusicItem :(YbrClassInfoItem *) classInfo albumname:(NSString *) albumname img:(NSString *) img type:(int) type albumId:(int) albumId
{
    return [YbrMusicItem classInfoToMusicItem:classInfo albumname:albumname singername:YbrDefualSingerName img:img type:type albumId:albumId];
}

+(YbrMusicItem *) classInfoToMusicItem :(YbrClassInfoItem *) classInfo albumname:(NSString *) albumname singername:(NSString *)singername img:(NSString *) img type:(int) type albumId:(int) albumId
{
    if(classInfo == nil) {
        return nil;
    }
    
    YbrMusicItem *item = [[YbrMusicItem alloc] init];
    
    item.musicId = classInfo.ID;
    item.musicName = classInfo.name;
    item.downloadurl = classInfo.audio_url;
    item.timeStr = classInfo.timelength;
    item.duration = classInfo.duration;
    item.file = classInfo.audio_url;
    item.albumId = albumId;
    item.singername = singername;
    item.albumname = albumname;
    item.img = img;
    item.type = type;
    
    return item;
}

+(YbrMusicItem *) textClassMusicToMusicItem :(YbrTextClassMusicItem *) textClassMusicInfo albumname:(NSString *) albumname singername:(NSString *)singername img:(NSString *) img type:(int) type albumId:(int) albumId
{
    
    if(textClassMusicInfo == nil) {
        return nil;
    }
    
    YbrMusicItem *item = [[YbrMusicItem alloc] init];
    
    item.musicId = textClassMusicInfo._id;
    item.musicName = textClassMusicInfo.name;
    item.downloadurl = textClassMusicInfo.audio_url;
//    item.url  = textClassMusicInfo.audio_url;
    item.timeStr = textClassMusicInfo.timelength;
    item.duration = [YbrTimerUtil makeTimeInt:item.timeStr];
    item.file = textClassMusicInfo.file;
    item.albumId = albumId;
    item.singername = singername;
    item.albumname = albumname;
    item.img = img;
    item.type = type;
    
    return item;
}


+(NSArray *) classInfosToMusicItems:(NSArray *) classInfos albumname:(NSString *) albumname singername:(NSString *)singername img:(NSString *) img type:(int) type  albumId:(int) albumId
{
    if(classInfos == nil) {
        return nil;
    }
    
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < classInfos.count; i ++) {
        [tempArr addObject:[YbrMusicItem classInfoToMusicItem:[classInfos objectAtIndex:i] albumname:albumname singername:singername img:img type:type albumId:albumId]];
    }
    return [tempArr copy];
}



+(NSArray *) classInfosToMusicItems:(NSArray *) classInfos albumname:(NSString *) albumname img:(NSString *)img type:(int) type albumId:(int) albumId{
    if(classInfos == nil) {
        return nil;
    }
    
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < classInfos.count; i ++) {
        [tempArr addObject:[YbrMusicItem classInfoToMusicItem:[classInfos objectAtIndex:i] albumname:albumname img:img type:type albumId:albumId]];
    }
    return [tempArr copy];
}



+(NSArray *) textClassInfoToMusicItem:(NSArray *) textClassInfos albumname:(NSString *)albumname singername:(NSString *)singername img:(NSString *) img type:(int) type  albumId:(int) albumId
{
    if(textClassInfos == nil)
    {
        return nil;
    }
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < textClassInfos.count; i ++)
    {
        NSArray *arr = ((YbrTextClassInfoItem *) textClassInfos[i]).audiolist;
        if(arr != nil)
        {
            for (int j = 0; j < [arr count]; j ++)
            {
                [tempArr addObject:[YbrMusicItem textClassMusicToMusicItem:((YbrTextClassMusicItem *)[arr objectAtIndex:j]) albumname:albumname singername:singername img:img type:type albumId:albumId]];
            }
        }
    }
    return [tempArr copy];
}


// 将音乐墙转换为播放列表
+(NSArray *) musicHallsToMusicItems:(NSArray *) musicInfos albumname:(NSString *) albumname type:(int) type albumId:(int) albumId
{
    if(musicInfos == nil)
    {
        return nil;
    }
    
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < musicInfos.count; i ++)
    {
        YbrMusicItem *item = [[YbrMusicItem alloc] init];
      
        id cellObj = musicInfos[i];
        if ((NSNull *)cellObj[@"id"] != [NSNull null]) {
            item.musicId = [cellObj[@"id"] intValue];
        }
        
        item.musicName = cellObj[@"name"];
        item.downloadurl = cellObj[@"url"];
        item.timeStr = [cellObj[@"timelength"] stringByReplacingOccurrencesOfString:@"." withString:@":"];;
        item.duration = [YbrTimerUtil makeTimeInt:item.timeStr];
        item.albumId = albumId;
        item.singername = YbrDefualSingerName;
        item.albumname = albumname;
        item.type = type;
        
        [tempArr addObject:item];
    }
    return [tempArr copy];
}

+(NSArray *)downMusicInfoChangeMusicArr:(NSArray *)musicArr FileType:(int)FileType
{
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSDictionary *dic in musicArr) {
        YbrMusicItem *item = [[YbrMusicItem alloc]init];
        item.musicId = [dic[@"id"] intValue];
        item.musicName = dic[@"name"];
        item.downloadurl = dic[@"url"];
        item.img = dic[@"img"];
        item.duration = (int)[YbrTimerUtil makeTimeInt:dic[@"duration"]];
        item.file = dic[@"url"];
        item.type = FileType;
        
        [tempArr addObject:item];
    }
    return tempArr;
}

@end
